from lib.share import ShareInfo, ShareDriver, ShareTicket_Info
import Login_12306_Seleuim as Login
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMessageBox, QTableWidgetItem, QCheckBox
from PyQt5 import QtCore


# 账户登录界面
class login_Ui:
    def __init__(self):
        self.ui = uic.loadUi('login_win.ui')
        self.ui.pushButton_login.clicked.connect(self.Bt_login_click)
        self.ui.lineEdit_password.returnPressed.connect(self.Bt_login_click)

    def Bt_login_click(self):
        ShareInfo.username = self.ui.lineEdit_username.text().strip()
        ShareInfo.password = self.ui.lineEdit_password.text().strip()
        check_type = self.ui.radioButton_slidercheck.isChecked()
        # 检验是否输入账号、密码
        if ShareInfo.username == " ":
            QMessageBox.Information(self, "提示", "请输入账户")
        elif ShareInfo.password == " ":
            QMessageBox.Information(self, "提示", "请输入密码")
        else:
            login = Login.login_12306(ShareInfo.username, ShareInfo.password)
            # 滑块验证(自动)
            if check_type:
                login_state = login.Signin_By_Slider()
                while login_state["state"] == "登录失败":
                    QMessageBox.Information(self, "提示", "滑块验证(自动)失败,请使用短信验证")
                    self.ui.show()
                # 验证成功，隐藏登录UI，显示车票信息UI-MainWindow12306
                else:
                    self.ui.close()
                    ShareInfo.ticketsinfo = Tickets_info_new()
                    ShareInfo.ticketsinfo.ui.show()
            # 短信验证
            else:
                # 登录界面隐藏，短信验证界面显示
                self.ui.close()
                ShareInfo.login_check = MessageCheck_Ui()
                ShareInfo.login_check.ui.show()


# 短信验证界面
class MessageCheck_Ui:
    def __init__(self):
        self.ui = uic.loadUi('message_check.ui')
        self.ui.pushButton_GetCode.clicked.connect(self.Getcode_Click)
        self.ui.pushButton_Commit.clicked.connect(self.Commit_Click)

    # 获取验证码
    def Getcode_Click(self):
        # 登录账号
        username = ShareInfo.username
        # 登录密码
        password = ShareInfo.password
        # 身份证后四位
        idcard = self.ui.lineEdit_IDCard.text().strip()
        # 身份证后四位验证
        message_check = Login.login_12306(username, password).Get_CheckCode(idcard)
        # 身份验证失败获取失败原因
        if message_check["state"] == "验证失败":
            QMessageBox.information(self.ui, "提示", message_check["content"])
        else:
            pass

    # 登录
    def Commit_Click(self):
        username = ShareInfo.username
        password = ShareInfo.password
        idcard = self.ui.lineEdit_IDCard.text().strip()
        checkcode = self.ui.lineEdit_CheckCode.text().strip()
        if checkcode:
            login_response = Login.login_12306(username, password).Signin_By_Message(idcard, checkcode)
            # 验证码验证通过，登录12306，隐藏短信验证界面，显示车票信息界面
            if login_response['state'] == '登录成功':
                self.ui.close()
                ShareInfo.ticketsinfo = Tickets_info_new()
                ShareInfo.ticketsinfo.ui.show()
            else:
                QMessageBox.information(self.ui, "提示", login_response['content'])
        else:
            QMessageBox.information(self.ui, "提示", "请输入验证码")


class Tickets_info_new:
    def __init__(self):
        self.ui = uic.loadUi('MainWindow12306.ui')
        # 显示登录者姓名
        self.ui.label_username.setText("您好，" + ShareDriver.login_user + "!")
        # 设置默认出发时间
        self.ui.dateEdit_startdate.setMinimumDate(QtCore.QDate.currentDate())
        # 查询按钮点击事件
        self.ui.pushButton_search.clicked.connect(self.Bt_search_click)
        # 抢票按钮点击事件
        self.ui.pushButton_Snatch.clicked.connect(self.snatch_ticket)

    # 点击查询按钮
    def Bt_search_click(self):
        start_station = self.ui.lineEdit_startstation.text().strip()
        end_station = self.ui.lineEdit_endstation.text().strip()
        start_date = self.ui.dateEdit_startdate.text().strip()
        # 高铁-城际
        GC_flag = self.ui.checkBox_GC.isChecked()
        # 动车
        D_flag = self.ui.checkBox_D.isChecked()
        # 直达
        Z_flag = self.ui.checkBox_Z.isChecked()
        # 特快
        T_flag = self.ui.checkBox_T.isChecked()
        # 快速
        K_flag = self.ui.checkBox_K.isChecked()
        # 其他
        Other_flag = self.ui.checkBox_Other.isChecked()

        if start_station and end_station:
            # 查询车票信息
            Login.Ticket_info_12306(start_station, end_station, start_date).search_ticketinfo(*(GC_flag, D_flag,
                                                                                                Z_flag, T_flag, K_flag,
                                                                                                Other_flag))
            # 获取查询车次数
            tickets_num = ShareTicket_Info.Tickets_num
            # print("车次次数：", tickets_num)
            # 设置表格行数
            self.table_rows(tickets_num)
            # 显示车票信息
            tickets_info = ShareTicket_Info.Tickets_Info
            self.ticketsinfo_showin_table(**tickets_info)
        else:
            QMessageBox.information(self.ui, "提示", '请输入出发地和目的地')

    # 设置表格行数
    def table_rows(self, row_num):
        ticket_table = self.ui.tableWidget_ticketsinfo
        # 清除表格所有行
        ticket_table.setRowCount(0)
        # 设置新的表格行
        ticket_table.setRowCount(row_num)
        # 设置表格列宽自动
        ticket_table.horizontalHeader().setStretchLastSection(True)

    # 车票信息显示至表格
    def ticketsinfo_showin_table(self, **ticketsinfo):
        ticket_table = self.ui.tableWidget_ticketsinfo
        ticket_table.setWordWrap(True)
        num = len(ticketsinfo)
        # 车次编号
        tickets_id = list(ticketsinfo.keys())
        # 车次信息
        tickets_info = list(ticketsinfo.values())
        # print('车次：', tickets_id)
        # print('车次信息：', tickets_info)
        # 车次
        for ticket_id in range(num):
            ticket_table.setCellWidget(ticket_id, 0, QCheckBox(tickets_id[ticket_id]))

        # 出发站-到达站
        for trains_stations_id in range(num):
            ticket_table.setItem(trains_stations_id, 1, QTableWidgetItem(tickets_info[trains_stations_id][0][0]
                                                                         + r'\n' + tickets_info[trains_stations_id][0]
                                                                         [1]))

        # 出发时间-到达时间
        for s_e_datetimes_id in range(num):
            ticket_table.setItem(s_e_datetimes_id, 2, QTableWidgetItem(tickets_info[s_e_datetimes_id][1][0]
                                                                       + r'\n' + tickets_info[s_e_datetimes_id][1][1]))

        # 历时
        for ls_id in range(num):
            ticket_table.setItem(ls_id, 3, QTableWidgetItem(tickets_info[ls_id][2][0]
                                                            + r'\n' + tickets_info[ls_id][2][1]))

        # 商务座、特等座
        for bussiness_seats_id in range(num):
            ticket_table.setItem(bussiness_seats_id, 4, QTableWidgetItem(tickets_info[bussiness_seats_id][3]))

        # 一等座
        for first_seats_id in range(num):
            ticket_table.setItem(first_seats_id, 5, QTableWidgetItem(tickets_info[first_seats_id][4]))

        # 二等座
        for second_seats_id in range(num):
            ticket_table.setItem(second_seats_id, 6, QTableWidgetItem(tickets_info[second_seats_id][5]))

        # 高级软卧
        for high_soft_berths_id in range(num):
            ticket_table.setItem(high_soft_berths_id, 7, QTableWidgetItem(tickets_info[high_soft_berths_id][6]))

        # 软卧
        for soft_berths_id in range(num):
            ticket_table.setItem(soft_berths_id, 8, QTableWidgetItem(tickets_info[soft_berths_id][7]))

        # 动卧
        for berths_id in range(num):
            ticket_table.setItem(berths_id, 9, QTableWidgetItem(tickets_info[berths_id][8]))

        # 硬卧
        for second_berths_id in range(num):
            ticket_table.setItem(second_berths_id, 10, QTableWidgetItem(tickets_info[second_berths_id][9]))

        # 软座
        for soft_seats_id in range(num):
            ticket_table.setItem(soft_seats_id, 11, QTableWidgetItem(tickets_info[soft_seats_id][10]))

        # 硬座
        for seats_id in range(num):
            ticket_table.setItem(seats_id, 12, QTableWidgetItem(tickets_info[seats_id][11]))

        # 无座
        for unfixed_seats_id in range(num):
            ticket_table.setItem(unfixed_seats_id, 13, QTableWidgetItem(tickets_info[unfixed_seats_id][12]))

        # 其他
        for other_id in range(num):
            ticket_table.setItem(other_id, 14, QTableWidgetItem(tickets_info[other_id][13]))

    # 抢票按钮点击事件
    def snatch_tickets():
        pass


if __name__ == "__main__":
    QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
    app = QApplication([])
    ShareInfo.login = login_Ui()
    ShareInfo.login.ui.show()
    app.exec_()
